//@charset "utf-8";
define(function(require, exports, module) {
	var Backbone = require('backbone');

	//键盘精灵
	var JpjlModel = require('../lib/jpjl');

	try {
		var jpjlModel = new JpjlModel({
			inputSelector: '#view_searchinput',
			appendToSelector: '#view_search_options_jpjl',
			//addBtnSelector : '#view_searchbtn',
			showListNum: 3
		});
		jpjlModel.render({
			left: 0,
			top: 26
		});
	} catch (e) {

	}

	var contentView = require('./content');
	var querySearchView = require('./querysearch');
	var stockSearchView = require('./stocksearch');
	var relationQueryView = require('./relationquery');
	var BaseView = require('./base');
	var baseView = new BaseView();

	var SearchView = Backbone.View.extend({
		el: '#view_div_searchbox',
		// 1 选股  2 联想
		searchType: 1,
		isInputChange: false,
		inputValue: '',
		events: {
			'keyup #view_searchinput': 'checkInputValue',
			'keydown #view_searchinput': 'listenInputDown',
			'blur #view_searchinput': 'blurInputValue',
			'focus #view_searchinput': 'focusInputValue',
			'click #view_searchbtn': 'doSearch',
			'click #view_searchinput': 'hiddenPlateOption'
		},
		initialize: function() {
			// 初始化search样式

			var placeholder = $('#view_searchinput').attr('data-placeholder');
			if(baseView.getQuery('plateid')){
				placeholder = '您可以输入股票代码或选股条件';
			}
			if (!baseView.getQuery('dynamicid')) {
				$('#view_searchinput').val(placeholder);
				$('#view_searchinput').css('color', '#9E9E9E');
			}
			$('#view_searchinput').blur();
			setTimeout(baseView.flash(), 500);
		},
		listenInputDown: function() {
			if (this.inputValue !== $('#view_searchinput').val()) {
				this.inputValue = $('#view_searchinput').val();
				this.isInputChange = true;
				return;
			}

			this.isInputChange = false;
		},
		checkInputValue: function(e) {
			e = e || window.event;
			$('#view_search_options_relation').hide();
			var inputValue = $('#view_searchinput').val().replace(/\s/g, '');
			var placeholder = $('#view_searchinput').attr('data-placeholder');
			if(baseView.getQuery('plateid')){
				placeholder = '您可以输入股票代码或选股条件';
			}
			if (placeholder === inputValue || '' === inputValue) {
				contentView.init();
				return;
			}

			if (e.keyCode === 13 && this.isInputChange) {
				if ('block' === $('#view_search_options_relation').css('display')) {
					$('#view_searchinput').val($('.active.view_relation_query_item').text());
				}
				this.doSearch();
				return;
			}

			if (null === jpjlModel.getInputCodeData()) {
				this.doRelationQuery(inputValue);
			}
		},
		focusInputValue: function() {
			TA.log({
				id: '57909977_357'
			});
			var placeholder = $('#view_searchinput').attr('data-placeholder');
			if(baseView.getQuery('plateid')){
				placeholder = '您可以输入股票代码或选股条件';
			}
			var inputValue = $('#view_searchinput').val().replace(/\s/g, '');
			$('#view_div_searchbox').addClass('searchbox-focus');
			$('#view_searchbtn').addClass('searchbtn-focus');

			if (placeholder === inputValue) {
				$('#view_searchinput').css('color', '#000');
				$('#view_searchinput').val('');
			}
			if (inputValue != '' && placeholder != inputValue) {
				$("#view_clearbtn").show();
			}
		},
		blurInputValue: function() {
			var placeholder = $('#view_searchinput').attr('data-placeholder');
			if(baseView.getQuery('plateid')){
				placeholder = '您可以输入股票代码或选股条件';
			}
			var inputValue = $('#view_searchinput').val().replace(/\s/g, '');
			$('#view_div_searchbox').removeClass('searchbox-focus');
			$('#view_searchbtn').removeClass('searchbtn-focus');

			if ('' === inputValue) {
				$('#view_searchinput').css('color', '#9E9E9E');
				$('#view_searchinput').val(placeholder);
				// if(baseView.getQuery('plateid')){
				// 	$('#view_searchinput').css('color', '#9E9E9E');
				// 	$('#view_searchinput').val('您可以输入股票代码或选股条件');
				// }
				return;
			}
		},
		doSearch: function() {
			var self = this;
			var placeholder = $('#view_searchinput').attr('data-placeholder');
			var inputValue = $('#view_searchinput').val().replace(/\s/g, '');
			if ('' === inputValue || placeholder === inputValue) {
				return;
			}

			//COCO新增
			//清除昨日前十自选股表格
			$('#tenstock').hide();
			// if(baseView.getQuery('plateid')){
			// 		$('#view_result_condition_create').hide();
			// 		$('#view_select_choose').hide();
			// 		$('#view_select_option_container').hide();
			// }

			self.resetTable();
			self.clearSearchResult();
			$('#view_searchinput').css('color', '#000');
			this.hideAllResultDiv();

			TA.log({
				id: '579097f4_415'
			});
			TA.log({
				id: 'zx_userput_0002',
				userput: inputValue
			});

			if (null === jpjlModel.getInputCodeData()) {
				$('#view_result_condition_second').empty();
				this.doQuerySearch(inputValue);
				return;
			} else {
				// 有股票代码的时候显示添加至下拉框
				$("#view_select_choose").show();
			}

			var stockCode = $('.autocomplete').find('.selected').find('strong').text();
			$('#view_searchinput').val(stockCode);
			this.doStockSearch(stockCode);
		},
		resetTable: function() {
			$(".sort").each(function() {
				$(this).html("");
			});
			$(".zdf").each(function() {
				$(this).parent().attr("type", "asc");
				$(this).html("↓");
			});
		},
		doStockSearch: function(str) {
			stockSearchView.request(str);
		},
		doQuerySearch: function(str) {
			querySearchView.request(str);
		},
		doRelationQuery: function(str) {
			relationQueryView.request(str);
		},
		hideAllResultDiv: function() {
			stockSearchView.hideEl();
			querySearchView.hideEl();
			relationQueryView.hideEl();

			$('#defaultView').hide();
			$('#view_result_condition_second').hide();
		},
		//隐藏板块选项
		hiddenPlateOption: function() {
			$("#view_select_option").hide();
			$('#view_select_option_container').hide();
		},
		//清理历史搜索结果
		clearSearchResult: function() {
			$("#view_code_list").html("");
			$("#view_result_condition_table_tbody").html("");
			$("#view_select_choose_name").addClass("add-plate-lock"); //恢复到初始状态
			$(".selectAll").each(function() {
				$(this).prop("checked", false);
			});
		}
	});

	module.exports = new SearchView();
});
